package edu.hawaii.halealohacli;

import java.util.AbstractSet;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Ranks the towers between two dates.
 * 
 * @author John MacMillan
 * @author Mitchell Kupfer
 * @author Bryson Hori
 * 
 */
public class RankTowers {
  String date1;
  String date2;
  WattDepotClient client;

  /**
   * Default constructor.
   * 
   * @param date1 the starting date.
   * @param date2 the ending date.
   * @param client the client.
   */
  public RankTowers(String date1, String date2, WattDepotClient client) {
    this.date1 = date1;
    this.date2 = date2;
    this.client = client;
  }

  /**
   * Runs the actual command.
   * 
   * @return a boolean, true if command is successful.
   * @throws Exception throws if command doesn't complete.
   */
  public boolean run() throws Exception {
    // return value, default false.

    // creates a list of sources and an iterator
    List<Source> sources = client.getSources();
    Iterator<Source> iter = sources.iterator();

    // trims strings
    date1 = date1.trim();
    date2 = date2.trim();

    // get the elements of the starting date
    String year1s = date1.substring(0, 4);
    int year1 = Integer.parseInt(year1s);
    String month1s = date1.substring(5, 7);
    int month1 = Integer.parseInt(month1s);
    String day1s = date1.substring(8, 10);
    int day1 = Integer.parseInt(day1s);

    // get the elements of the end date
    String year2s = date2.substring(0, 4);
    int year2 = Integer.parseInt(year2s);
    String month2s = date2.substring(5, 7);
    int month2 = Integer.parseInt(month2s);
    String day2s = date2.substring(8, 10);
    int day2 = Integer.parseInt(day2s);

    // starting calendar date
    GregorianCalendar calandar = new GregorianCalendar();
    calandar.set(GregorianCalendar.YEAR, year1);
    calandar.set(GregorianCalendar.MONTH, month1 - 1);
    calandar.set(GregorianCalendar.DAY_OF_MONTH, day1);
    calandar.set(GregorianCalendar.HOUR, 1);
    calandar.set(GregorianCalendar.MINUTE, 0);
    calandar.set(GregorianCalendar.SECOND, 0);

    XMLGregorianCalendar start = DatatypeFactory.newInstance().newXMLGregorianCalendar(calandar);

    // ending calendar date
    calandar.set(GregorianCalendar.YEAR, year2);
    calandar.set(GregorianCalendar.MONTH, month2 - 1);
    calandar.set(GregorianCalendar.DAY_OF_MONTH, day2);
    calandar.set(GregorianCalendar.HOUR, 23);
    calandar.set(GregorianCalendar.MINUTE, 59);
    calandar.set(GregorianCalendar.SECOND, 59);

    XMLGregorianCalendar end = DatatypeFactory.newInstance().newXMLGregorianCalendar(calandar);

    // new storage tree to help with storing and printing.
    Storage stores = new Storage();

    while (iter.hasNext()) {

      Source sorce = iter.next();
      String sorceName = sorce.getName();

      // get the energy consumed between dates.
      double energyConsumed = client.getEnergyConsumed(sorceName, start, end, 15);

      // store each name and energy used between the dates.
      stores.putInfo(sorceName, (long) energyConsumed);
    }

    AbstractSet<Nod> tree = stores.getInfo();
    Iterator<Nod> iterate = tree.iterator();

    String sourceTitle = "Source";
    String descriptTitle =
        "For the interval " + date1 + ", " + date2 + " energy consumption by tower was: ";
    System.out.format("%-20s%s%n", sourceTitle, descriptTitle);

    while (iterate.hasNext()) {
      Nod nody = iterate.next();
      nody.number = nody.number / 1000;
      System.out.format("%-20s", nody.name);
      System.out.format("%s %n", nody.number + " kWh");
    }

    return true;
  }

}
